package com.pxmeta.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.circle.entity.Circle;
import com.pxmeta.circle.service.ICircleService;
import com.pxmeta.circle.service.IUserCircleService;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.UserIdentify;
import com.pxmeta.user.entity.UserStudent;
import com.pxmeta.user.mapper.UserStudentMapper;
import com.pxmeta.user.service.IUserIdentifyService;
import com.pxmeta.user.service.IUserService;
import com.pxmeta.user.service.IUserStudentService;
import com.pxmeta.verify.entity.StuInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-07-03
 */
@Service
@RequiredArgsConstructor
public class UserStudentServiceImpl extends ServiceImpl<UserStudentMapper, UserStudent> implements IUserStudentService {

    private final IUserService userService;
    private final IUserIdentifyService userIdentifyService;
    private final ICircleService circleService;

    private final static Integer IS_STUDENT = 1;

    /**
     * 保存用户学生身份信息
     * @param userId
     * @param info
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveStudent(Long userId, StuInfo info) {
        String university = info.getUniversity();
        Circle circle = circleService.getBySchoolName(university);
        if (null == circle){
            throw new ServiceException("您的就读学校圈子未开放，请联系客服！");
        }
        UserStudent student = new UserStudent();
        student.setUserId(userId);
        student.setDepartment(info.getDepartment());
        student.setDomain(info.getDomain());
        student.setStuNum(info.getStuNum());
        String status = info.getStatus();
        if (status.equals("已毕业")){
            student.setStatus(1);
        }else {
            student.setStatus(0);
        }
        student.setStuClass(info.getSClass());
        student.setLevel(info.getLevel());

        student.setUniversity(university);
        student.setIsFullTime("全日制".equals(info.getForm())? 1:0);
        this.saveOrUpdate(student);
        User user = new User();
        user.setId(userId);
        user.setSchoolName(info.getUniversity());
        user.setIsStudent(1);
        userService.updateById(user);

        UserIdentify userIdentify = new UserIdentify();
        userIdentify.setUserId(userId);
        userIdentify.setType(1);
        userIdentify.setIdType("学信网在线认证");
        userIdentify.setOrgName(university);
        userIdentify.setStatus(1);
        userIdentifyService.save(userIdentify);
        circleService.handleVerifyStudentSuccess(userId);
    }

    @Override
    public void identifyByImg(Long userId, List<String> imgUrls, String orgName) {
        UserIdentify userIdentify = new UserIdentify();
        userIdentify.setImgUrls(StrUtil.join(",",imgUrls));
        userIdentify.setUserId(userId);
        userIdentify.setType(1);
        userIdentify.setIdType("证件认证");
        userIdentify.setOrgName(orgName);
        userIdentify.setStatus(0);
        userIdentifyService.save(userIdentify);
    }

    @Override
    public UserIdentify getIdentifyStatus(Long userId) {
        LambdaQueryWrapper<UserIdentify> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserIdentify::getUserId, userId)
                .eq(UserIdentify::getType,1);

        return userIdentifyService.getOne(wrapper, false);
    }

    @Override
    public UserStudent getStudentInfo(Long userId) {
        User user = userService.getById(userId);
        if (!IS_STUDENT.equals(user.getIsStudent())){
            return null;
        }
        return this.getById(userId);
    }
}
